<?php
/**
 * Created by PhpStorm.
 * User: idcu
 * Date: 2016/3/12
 * Time: 10:43
 */
namespace Assistant\Controller;
use Admin\Controller\AdminController;

class WxOrderController extends AdminController
{
    public function orderList(){
        //列表
        $model = D('Service/WxOrder');
        $step = I('step');
        $handler = I('handler');
        if($step==10)
            $step=false;
        $data = $model->selectStep(false,$step,10,false, $wecha_id=false, $handler);

        $this->assign('data', $data['data']);
        $this->assign('page', $data['page']);
        $this->display();
    }

    public function roll(){
        $id = I('id');
        $step = I('step');
        if($step==10)
            $step=false;
        $data = D('Service/WxOrder')->roll($id, $step);
        if($data){
            $res['status'] = 1;
            $res['data'] = $data;
            $this->ajaxReturn($res);
        }else{
            $res['status'] = false;
            $this->ajaxReturn($res);
        }
    }

    public function rob(){
        $ok = order_confirm(I('id'),1);
        if(!$ok)
            $this->error('订单状态已变！');
        $id = I('id');
        $ok = D('Service/WxOrder')->rob($id);
        if($ok)
            $this->redirect('Service/WxOrder/orderList',array('step'=>10));
        else
            $this->error('设置失败！');
    }

    public function cancel(){
        $this->display();
    }

    public function off(){
        $ok = order_confirm(I('id'),1);
        if(!$ok){
            $res['status'] = false;
            $res['msg'] = '订单状态已改变';
            $this->ajaxReturn($res);
        }
        $id = I('id');
        $remark = I('remark');
        $ok = D('Service/WxOrder')->off($id,$remark);
        if($ok){
            $res['status'] = 1;
            $this->ajaxReturn($res);
        }
        $res['status'] = false;
        $res['msg'] = '设置失败';
        $this->ajaxReturn($res);
    }

    public function delete(){
        $id = I('get.id');
//        myDump($id);
        $ok = D('Service/WxOrder')->delete($id);
        if($ok)
            $res['status'] = 1;
        else
            $res['status'] = false;
        $str = $ok?'成功':'失败';
        $res['msg'] = '删除'.$str;
        $this->ajaxReturn($res);
    }

    public function appoint(){
        $id = I('id');
        $data = D('Service/WxOrder')->repairManList($id);
//        myDump($data);die;
        $this->assign('data',$data);
        $this->display();
    }
    public function choose(){
        $ok = order_confirm(I('oid'),1);
        if(!$ok){
            $res['status'] = false;
            $res['msg'] = '订单状态已改变';
            $this->ajaxReturn($res);
        }
        $oid = I('oid');
        $mid = I('mid');
        $ok = D('Service/WxOrder')->appoint($oid, $mid);
        if($ok){
            $res['status'] = 1;
            $this->ajaxReturn($res);
        }
        $res['status'] = false;
        $res['msg'] = '指派失败';
        $this->ajaxReturn($res);
    }

    public function info(){
        $id = I('id');
        $data = D('Service/WxOrder')->getInfo($id);
        if($data['createtime'])
            $data['create_time'] = date('Y-m-d H:i:S',$data['createtime']);
//        myDump($data);die;
        $this->assign('data',$data);
        $this->display();
    }

    public function send(){
//        die;
        $ok = D('Service/WxOrder')->adding();
        if($ok){
            $res['status'] = 1;
            $this->ajaxReturn($res);
        }
        $res['status'] = false;
        $res['msg'] = '创建失败';
        $this->ajaxReturn($res);
    }

    public function create()
    {
        $req = $this->getReq();
        $customer = D('Customer')->relation('families')->find($req['id']);
        $this->assign('customer',$customer);
        $this->display();
    }

    public function dumpExcel(){
//        $req = I('');
//        dd($req);
        $step = I('step');
        if($step==10)
            $step=false;
        $order = D('Service/WxOrder')->selectStep(false,$step,999,false, $wecha_id=false, false);
        $title = array('订单编号','项目','工单状态','工单大类','工单细类','事件说明','报修人','报修人电话','地址','创建时间','抢单开始时间','维修工','维修工电话','处理客服','评价');
        foreach ($order['data'] as $k=>$v) {
            $data[$k][] = '编号'.$v['serialno'] ;
            $data[$k][] = $v['community_name'] ;
            $data[$k][] = $v['step_title'] ;
            $data[$k][] = $v['type_name'] ;
            $data[$k][] = $v['entry_name'] ;
            $data[$k][] = $v['info'] ;
            $data[$k][] = $v['customer']['realname'] ;
            $data[$k][] = $v['customer']['tel'] ;
            $data[$k][] = $v['address'] ;
            $data[$k][] = $v['create_time'] ;
            $data[$k][] = $v['rob_start_date'] ;
            $data[$k][] = $v['handler']['name'] ;
            $data[$k][] = $v['handler']['mobile'] ;
            $data[$k][] = $v['service_name'] ;
            $data[$k][] = $v['assess'] ;
        }
//        myDump($data);die;
        dumpExcel($title, $data,'维修订单'.date('Y-m-d'));
    }

    public function done(){
        $res = D('Service/WxOrder')->done();
        $step = I('step');
        $step=$step?$step:10;
        $tel = I('tel');
        if($res)
            $this->redirect(U('orderList',array('tel'=>$tel,'step'=>$step)));
        else
            $this->error('完成失败');
    }

    public function edit(){
        $res = D('Service/WxOrder')->edit();
        $this->ajaxReturn($res);
    }
}